// pages/my/my.js
const app = getApp()
//const user = JSON.parse(wx.getStorageSync("user"));
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '我的'
    })
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.hideLoading();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    let refresh = true
    this.init(refresh)
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  init:function(refresh = false){
    wx.showLoading({
      title: '加载中',
      success:()=>{
        wx.request({
          url: base_url + '/Users/userInfo',
          method: 'POST',
          data: {
            'user_token': JSON.parse(wx.getStorageSync("user")).user_token
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: res => {
            if (res.data.success == '1') {
              this.setData({
                user: res.data.msg
              })
            }else{
              wx.showToast({
                title: '加载失败',
                icon: 'none',
                duration: 2000
              })
            }
            refresh == true && wx.stopPullDownRefresh(); 
            wx.hideLoading();
          }
        })
      }
    })
   
  },
  goMy:function(e){//跳转
    let i = e.currentTarget.dataset.idx;
    wx.navigateTo({ url: '/pages/myPost/myPost?idx='+i })
  },
  goRecode(e){
    let i = e.currentTarget.dataset.idx;
    i == 0 && wx.navigateTo({ url: '/pages/reward/reward' });
    i == 1 && wx.navigateTo({ url: '/pages/love/love',})
    i == 2 && wx.navigateTo({ url: '/pages/myProblem/myProblem' });
    i == 3 && wx.navigateTo({ url: '/pages/aboutUs/aboutUs' });
    i == 5 && wx.navigateTo({ url: '/pages/myht/myht' });
    if(this.data.user.phone){
      i == 4 && wx.navigateTo({ url: '/pages/tel/tel?phone='+this.data.user.phone });
    }else{
      i == 4 && wx.navigateTo({ url: '/pages/tel/tel' });
    }
  }
})